<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="商品名称" prop="name">
                    <el-input
                        v-model="queryParams.name"
                        placeholder="请输入商品名称"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="状态" prop="status">
                    <el-select v-model="queryParams.status" style="width: 200px" placeholder="请选择状态" clearable>
                      <el-option
                          v-for="dict in mall_status"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                      />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="创建时间" prop="createdAt">
                    <el-date-picker clearable
                                    v-model="queryParams.createdAt"
                                    type="date"
                                    value-format="YYYY-MM-DD"
                                    placeholder="请选择创建时间">
                    </el-date-picker>
                  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['mall:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['mall:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['mall:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['mall:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="商品名称" align="center" prop="name" width="180" v-if="columns[1].visible" :show-overflow-tooltip="true"/>
                <el-table-column label="商品描述" align="center" prop="description" width="180" v-if="columns[2].visible" :show-overflow-tooltip="true"/>
                <el-table-column label="所需积分" align="center" prop="pointsRequired" v-if="columns[3].visible" :show-overflow-tooltip="true"/>
                <el-table-column label="库存" align="center" prop="stock" v-if="columns[4].visible" :show-overflow-tooltip="true"/>
                <el-table-column label="状态" align="center" prop="status" v-if="columns[5].visible">
                <template #default="scope">
                      <dict-tag :options="mall_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
<!--                <el-table-column label="商品图片" align="center" prop="image" width="100" v-if="columns[6].visible">-->
<!--                <template #default="scope">-->
<!--                  <image-preview :src="scope.row.image" :width="50" :height="50"/>-->
<!--                </template>-->
<!--              </el-table-column>-->
                <el-table-column label="创建时间" align="center" prop="createdAt" width="120" v-if="columns[7].visible" :show-overflow-tooltip="true">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
                <el-table-column label="更新时间" align="center" prop="updatedAt" width="120" v-if="columns[8].visible" :show-overflow-tooltip="true">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mall:goods:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mall:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改积分商品对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="goodsRef" :model="form" :rules="rules" label-width="80px">
                        <el-form-item label="商品名称" prop="name">
                          <el-input v-model="form.name" placeholder="请输入商品名称" />
                        </el-form-item>
                        <el-form-item label="商品描述" prop="description">
                          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                        <el-form-item label="状态" prop="status">
                          <el-select v-model="form.status" placeholder="请选择状态">
                            <el-option
                                v-for="dict in mall_status"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                            ></el-option>
                          </el-select>
                        </el-form-item>
<!--                        <el-form-item label="商品图片" prop="image">-->
<!--                          <image-upload v-model="form.image"/>-->
<!--                        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Goods">
  import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/mall/goods";

  const { proxy } = getCurrentInstance();
      const { mall_status } = proxy.useDict('mall_status');

  const goodsList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
                    name: null,
                    stock: null,
                    status: null,
                    createdAt: null,
    },
    rules: {
                    name: [
                { required: true, message: "商品名称不能为空", trigger: "blur" }
              ],
                    pointsRequired: [
                { required: true, message: "所需积分不能为空", trigger: "blur" }
              ],
                    stock: [
                { required: true, message: "库存不能为空", trigger: "blur" }
              ],
                    status: [
                { required: true, message: "状态不能为空", trigger: "change" }
              ],
                    image: [
                { required: true, message: "商品图片不能为空", trigger: "blur" }
              ],
    },
    //表格展示列
    columns: [
              { key: 0, label: '${comment}', visible: true },
                { key: 1, label: '商品名称', visible: true },
                { key: 2, label: '商品描述', visible: true },
                { key: 3, label: '所需积分', visible: true },
                { key: 4, label: '库存', visible: true },
                { key: 5, label: '状态', visible: true },
                { key: 6, label: '商品图片', visible: true },
                { key: 7, label: '创建时间', visible: true },
                { key: 8, label: '更新时间', visible: true },
      ],
  });

  const { queryParams, form, rules,columns } = toRefs(data);

  /** 查询积分商品列表 */
  function getList() {
    loading.value = true;
    listGoods(queryParams.value).then(response => {
            goodsList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
                    goodsId: null,
                    name: null,
                    description: null,
                    pointsRequired: null,
                    stock: null,
                    status: null,
                    image: null,
                    createdAt: null,
                    updatedAt: null
    };
    proxy.resetForm("goodsRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.goodsId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加积分商品";
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _goodsId = row.goodsId || ids.value
    getGoods(_goodsId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改积分商品";
    });
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["goodsRef"].validate(valid => {
      if (valid) {
        if (form.value.goodsId != null) {
          updateGoods(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addGoods(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _goodsIds = row.goodsId || ids.value;
    proxy.$modal.confirm('是否确认删除积分商品编号为"' + _goodsIds + '"的数据项？').then(function() {
      return delGoods(_goodsIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('mall/goods/export', {
      ...queryParams.value
    }, `goods_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
